const path = require("path")
const glob = require("glob")
const webpack = require("webpack")
const uglify = require('uglifyjs-webpack-plugin')
const htmlPlugin = require("html-webpack-plugin")
const extractTextPlugin = require("extract-text-webpack-plugin")
const PurifyCSSPlugin = require("purifycss-webpack")
const copyWebpackPlugin = require("copy-webpack-plugin")
const entry = require("./webpack_config/entry_webpack.js")

console.log(encodeURIComponent(process.env.type))

if (process.env.type == "build") {
    var website = {
        publicPath: "http://www.ckk.com/"
    }
} else {
    var website = {
        publicPath: "http://192.168.1.88:1812/"
    }
}
module.exports = {
    // devtool: 'eval-source-map',
    entry: entry.path,
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: '[name].js',
        publicPath: website.publicPath
    },
    resolve: {
        alias: {
            'vue': 'vue/dist/vue.js'
        }
    },
    module: {
        rules: [
            {
                test: /\.css$/,
                // use: ["style-loader", "css-loader"], // 
                /* use: [{ //use的json格式编写
                    loader: 'style-loader'
                },{
                    loader: 'css-loader'
                }] */
                use: extractTextPlugin.extract({
                    fallback: "style-loader",
                    use: [
                        {"loader": 'css-loader', options: {importLoaders: 1}},
                        "postcss-loader"
                    ]
                })
                /* use: [
                    {
                        loader: "style-loader"
                    },{
                        loader: "css-loader",
                        options: {
                            modules: true
                        }
                    },{
                        loader: "postcss-loader"
                    }
                ] */
            },{
                test: /\.(png|jpg|gif)/,
                use: [{
                    loader: 'url-loader',
                    options: {
                        limit: 50000,
                        outputPath: 'image/'
                    }
                }]
            },{
                test: /\.(htm|html)$/i,
                use: ['html-withimg-loader']
            },{
                test: /\.less$/,
                use: [{
                    loader: "style-loader"
                },{
                    loader: "css-loader"
                },{
                    loader: "less-loader"
                }],
                /* use: extractTextPlugin.extract({
                    use: [{
                        loader:"css-loader"
                    },{
                        loader: "less-loader"
                    }],
                    fallback: "style-loader"
                }) */
            },{
                test: /\.(jsx|js)$/,
                use: {
                    loader: 'babel-loader'
                },
                exclude: /node_modules/
            }
        ]
    },
    plugins: [
        // 打包出静态文件
        new copyWebpackPlugin([{
            from: __dirname+'/src/public',
            to: './public'
        }]),
        // 分离js
        new webpack.optimize.CommonsChunkPlugin({
            name: ['vue', 'jquery'],
            filename: 'assets/js/[name].js',
            minChunks: 2
        }),
        // 全局引入第三方插件
        new webpack.ProvidePlugin({
            $: 'jquery',
            Vue: 'vue'
        }),
        // 打包后压缩JS
        // new uglify(),

        // 静态输出src下的 html文件
        new htmlPlugin({
            minify: {
                removeAttributeQuotes: true
            },
            hash: true,
            template: './src/index.html'
        }),

        // 打包抽离出css
        new extractTextPlugin("/css/index.css"),

        // 消除无用css的插件
        new PurifyCSSPlugin({
            paths: glob.sync(path.join(__dirname, 'src/*.html'))
        }),

        /* 版权声明 */
        new webpack.BannerPlugin('ckk版权所有')
    ],
    devServer: {
        // 配置服务器基本运行路径，用于找到程序打包地址
        contentBase: path.resolve(__dirname, 'dist'),
         // 服务器运行地址，建议使用本机IP
        host: '192.168.1.88',
        // 服务器端压缩选型， 一般设置为开启。
        compress: true,
        open: true,
        // 配置运行端口，建议不使用80,很容易被占用，我这里使用1812
        port: 1812
    },
    watchOptions: {
        /* 检测修改的时间，以毫秒为单位 */
        poll: 1000,
        // 防止重复保存尔发生重复编译错误，这里设置的500是半秒重复保存，不进行打包错作
        aggregateTimeout: 500,
        // 不监听的目录
        ignored: /node_modules/,
    }
}